package cn.wolfcde.luowowo.website.web.controller;

import cn.wolfcde.luowowo.website.annotation.LoginRequired;
import cn.wolfcde.luowowo.website.annotation.RedisParameter;
import cn.wolfcde.luowowo.website.util.CookieUtil;
import cn.wolfcde.luowowo.website.util.UMEditorUploader;
import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelQueryObject;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisticsService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisticsService;
import cn.wolfcode.luowowo.comment.service.ITravelCommentServiceService;
import cn.wolfcode.luowowo.common.domain.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class IndexController {

    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IDestinationTemplateSearchService destinationTemplateSearchService;

    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateSearchService;

    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;

    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;

    @Reference
    private ISearchService searchService;


    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index(Model model) {

        List<TravelCommend> top5 = travelCommendService.getTop5();

        model.addAttribute("tcs", top5);

        //推荐一个热门攻略
        List<StrategyCommend> s_top5 = strategyCommendService.getTop5();

        model.addAttribute("sc", s_top5.get(0));

        return "index/index";

    }

    /**
     * 返回推荐热门游记和最新发表游记
     */
    @RequestMapping("index/travelPage")
    public String travelPage(@ModelAttribute("qo") TravelQueryObject qo, Model model) {

        PageInfo pageInfo = travelService.queryForList(qo);

        model.addAttribute("pageInfo", pageInfo);

        return "index/travelPageTpl";
    }

    /**
     * 搜索功能
     *
     * @param qo
     * @return
     */
    @RequestMapping("/q")
    public String travelPage(@ModelAttribute("qo") SearchQueryObject qo, Model model) {

        if (qo.getType() == SearchQueryObject.DEST) {
            return this.searchByDestNameOrInfo(qo, model);
        } else if (qo.getType() == SearchQueryObject.STRATEGY) {
            return this.searchStrategy(qo, model);
        } else if (qo.getType() == SearchQueryObject.TRAVEL) {
            return this.searchTravel(qo, model);
        } else if (qo.getType() == SearchQueryObject.USERINFO) {
            return this.searchUserInfo(qo, model);
        } else {
            return this.searchAll(qo, model);
        }
    }

    private String searchAll(SearchQueryObject qo, Model model) {

        //全都进行一次全文搜索;
        Page<UserInfoTemplate> u_page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");

        Page<TravelTemplate> t_page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");

        Page<StrategyTemplate> s_page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary");

        Page<DestinationTemplate> d_page = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME,DestinationTemplate.TYPE_NAME,DestinationTemplate.class,qo,"name","info");


        //将结果使用vo对象进行接收
        SearchResultVO vo = new SearchResultVO();

        vo.setDests(d_page.getContent());
        vo.setStrategys(s_page.getContent());
        vo.setTravels(t_page.getContent());
        vo.setUsers(u_page.getContent());

        vo.setTotal(d_page.getContent().size() + s_page.getContent().size() + u_page.getContent().size() + t_page.getContent().size());

        model.addAttribute("data",vo);

        return "index/searchAll";
    }

    private String searchUserInfo(SearchQueryObject qo, Model model) {

        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");

        model.addAttribute("page",page);

        return "index/searchUser";
    }

    private String searchTravel(SearchQueryObject qo, Model model) {

        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");

        model.addAttribute("page",page);

        return "index/searchTravel";
    }

    private String searchStrategy(SearchQueryObject qo, Model model) {

        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary");

        model.addAttribute("page",page);

        return "index/searchStrategy";
    }

    /**
     * 目的地搜索  搜索出目的地地相关的攻略,游记,用户信息
     * @param qo
     * @param model
     * @return
     */
    private String searchByDestNameOrInfo(SearchQueryObject qo, Model model) {

       DestinationTemplate destinationTemplate = destinationTemplateSearchService.searchByDestName(qo.getKeyword());

       if(destinationTemplate != null){
            //根据destName 查询获取到  用户,攻略 ,游记 跟此目的地相关的 模板数据集合
           List<UserInfoTemplate> userInfoTemplates = userInfoTemplateSearchService.findByDestName(destinationTemplate.getName());

            //对显示条数做一个控制
           if(userInfoTemplates.size() > 5){

               userInfoTemplates.subList(0,5);

           }

           List<StrategyTemplate> strategyTemplates = strategyTemplateSearchService.findByDestName(destinationTemplate.getName());

           //对显示条数做一个控制
           if(strategyTemplates.size() > 5){

               strategyTemplates.subList(0,5);

           }

           List<TravelTemplate> travelTemplates = travelTemplateSearchService.findByDestName(destinationTemplate.getName());

           //对显示条数做一个控制
           if(travelTemplates.size() > 5){

               travelTemplates.subList(0,5);

           }

           //统计一共有多少条相关数据
           int total = userInfoTemplates.size() + strategyTemplates.size() + travelTemplates.size();

           SearchResultVO vo = new SearchResultVO();

           vo.setStrategys(strategyTemplates);
           vo.setTravels(travelTemplates);
           vo.setUsers(userInfoTemplates);
           vo.setTotal(total);

           model.addAttribute("data",vo);

       }

        model.addAttribute("dest",destinationTemplate);

        return  "index/searchDest";
    }


}
